//牛客 WY28 跳石板
#include <iostream>
#include<vector>
#include<limits.h>
#include<math.h>
using namespace std;

void div_num(int v,vector<int> &a)
{
    for(int i=2;i<=sqrt(v);i++)
    {
        if(v%i==0)
        {
            a.push_back(i);
            if(v/i!=i)
            {
                a.push_back(v/i);
            }
        }
    }
}

int jump(int n,int m)
{
    vector<int> step(m+1,INT_MAX);
    step[n]=0;
    for(int i=n;i<m;i++)
    {
        if(step[i]==INT_MAX)
        {
            continue;
        }
        vector<int> a;

        div_num(i, a);

        for(int j=0;j<a.size();j++)
        {
            if(a[j]+i<=m && step[a[j]+i]!=INT_MAX)
            {
                step[a[j]+i]=step[a[j]+i]<step[i]+1 ? step[a[j]+i] : step[i]+1;
            }
            else if(a[j]+i<=m)
            {
                step[a[j]+i] =step[i]+1;
            }
        }
    }
    return step[m]==INT_MAX ? -1 :step[m];
}


int main() {
    int n,m,min_step;
    while (cin>>n>>m) {
        min_step=jump(n, m);
        cout<<min_step<<endl;
    }
    return 0;
}